﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Common;

public partial class shopping_order : MemberPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        PageBegin();

        /// 保存设置
        if (RequestBase.GetFormString("___EVENTTARGET") == "submitorder")
        {
            SubmitOrderInfo();
        }

        if (!IsPostBack)
        { 
            //emTotalCost.InnerHtml = Profile.CardItems.TotalCost.ToString();
            strong_PayTotalAmount.InnerHtml = Profile.CardItems.TotalCost.ToString();
        } 
    } 

    #region 对象初始化
    /// <summary>
    /// 送货地址簿
    /// </summary>
    Model.ShippingAddressBase AddressObj
    {
        get
        {
            Model.ShippingAddressBase _addressObj = new Model.ShippingAddressBase();

            if (!string.IsNullOrEmpty(RequestBase.GetFormString("txtShippingAddressId")))
                _addressObj.ShippingAddressId = RequestBase.GetFormString("txtShippingAddressId");

            _addressObj.ShipAddressName = RequestBase.GetFormString("txtShipAddressName");
            _addressObj.ShipAddress = RequestBase.GetFormString("txtShipAddress");
            _addressObj.ShipCounties = RequestBase.GetFormString("txtShipCounties");
            _addressObj.ShipCity = RequestBase.GetFormString("txtShipCity");
            _addressObj.ShipState = RequestBase.GetFormString("txtShipState");
            _addressObj.ShipCountry = "中国";
            _addressObj.ShipZip = RequestBase.GetFormString("txtShipZip");
            _addressObj.ShipTel = RequestBase.GetFormString("txtShipTel");
            _addressObj.ShipMobile = RequestBase.GetFormString("txtShipMobile");
            _addressObj.ShipToName = RequestBase.GetFormString("txtShipToName");
            _addressObj.ShipCountiesId = int.Parse((RequestBase.GetFormString("txtShipCountiesId") == "") ? "0" : RequestBase.GetFormString("txtShipCountiesId"));
            _addressObj.ShipCityId = int.Parse((RequestBase.GetFormString("txtShipCityId") == "") ? "0" : RequestBase.GetFormString("txtShipCityId"));
            _addressObj.ShipStateId = int.Parse((RequestBase.GetFormString("txtShipStateId") == "") ? "0" : RequestBase.GetFormString("txtShipStateId"));
            _addressObj.ShipCountryId = 0;
            _addressObj.UserId = this.UserId;
            _addressObj.StatusCode = 0;

            return _addressObj;
        }
    }

    /// <summary>
    /// 订单实体
    /// </summary>
    Model.OrderBase OrderObj
    {
        get
        {
            Model.OrderBase _orderObj = new Model.OrderBase();

            _orderObj.ShippingAddressId = RequestBase.GetFormString("txtShippingAddressId");
            _orderObj.ShipAddress = RequestBase.GetFormString("hidShipAddress");
            _orderObj.ShipCounties = RequestBase.GetFormString("hidShipCounties").Split('|')[2];
            _orderObj.ShipCity = RequestBase.GetFormString("hidShipCounties").Split('|')[1];
            _orderObj.ShipState = RequestBase.GetFormString("hidShipCounties").Split('|')[0];
            _orderObj.ShipCountry = "中国";
            _orderObj.ShipZip = RequestBase.GetFormString("hidShipZip");
            _orderObj.ShipTel = RequestBase.GetFormString("hidShipTel");
            _orderObj.ShipMobile = RequestBase.GetFormString("hidShipMobile");
            _orderObj.ShipToName = RequestBase.GetFormString("hidShipToName");
            _orderObj.UserId = this.UserId;
            _orderObj.StatusCode = 0;
            _orderObj.PaymentTypeId = 0;
            _orderObj.PaymentType = "integral";
            _orderObj.NeedInvoice = false;
            _orderObj.InvoiceTitle = "";
            _orderObj.InvoiceType = "";
            _orderObj.Locale = "中文";
            _orderObj.CreatedBy = this.UserId;
            _orderObj.ModifyBy = this.UserId;
            _orderObj.CreatedOn = DateTime.Now;
            _orderObj.ModifyOn = DateTime.Now;
            _orderObj.ShippingCost = 0;
            _orderObj.ShippingMethod = "快递";
            _orderObj.ShippingMethodType = 1;
            _orderObj.TotalPrice = Profile.CardItems.TotalCost;
            _orderObj.OrderDate = DateTime.Now;
            _orderObj.OrderItems = Profile.CardItems.GetOrderItems();
            _orderObj.Comment = RequestBase.GetFormString("txtComments");
            return _orderObj;
        }
    }
    #endregion

    #region 保存数据
    void SubmitOrderInfo()
    {
        string res = BLL.OrderBase.OrderCreate(OrderObj);

        switch (res.Split('|')[0])
        {
            case "succeeded":
                Profile.CardItems.Clear();
                Profile.Save();
                JScript.AlertAndRedirect("保存成功!", string.Format("shopping_checkout.aspx?orderid={0}", res.Split('|')[1]));
                break;
            default:
                JScript.Alert(res);
                break;
        }
    }
    #endregion
}